#include "ConfirmFunctions.h"
#include <string>
using namespace std;

int confirmFunctions::findFunction(string name) {
    for(int i = 0; i < n_functions; i++) {
        if(all_functions[i]->get_decl()->get_name().str() == name) {
            return i;
        }
    }
    return -1;
}

int confirmFunctions::addFunction(SgFunctionDeclaration *decl) {
    if(findFunction(decl->get_name().str()) != -1) return findFunction(decl->get_name().str());
    all_functions = (FunctionContext **)realloc(all_functions, (n_functions + 1) * sizeof(FunctionContext *));
    all_functions[n_functions] = new FunctionContext(decl,all);
    n_functions = n_functions + 1;
    return n_functions-1;
}

void confirmFunctions::preOrderVisit(SgNode *node ){
    SgFunctionDefinition *func_def = isSgFunctionDefinition(node);
    SgFunctionDeclaration *func_decl = isSgFunctionDeclaration(node);

    if(func_decl != NULL ) {
        addFunction(func_decl);
    } else if(func_def != NULL) {
        int current_function_index = findFunction(func_def->get_declaration()->get_name().str());
        all_functions[current_function_index]->confirmFunction();
    }
}
void confirmFunctions::postOrderVisit(SgNode *node ){
}

